package com.chilly.io;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by Chilly Cui on 2020/9/24.
 */
public class InputStreamDemo {

    @Test
    void FileInputStreamTest3() throws IOException {
        File file = new File("c:/env/诸葛亮.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        // 核心代码
        byte[] data = new byte[2];
        int len;
        while ((len = fileInputStream.read(data)) != -1) {
            // len 为每次读取的有效的字节个数
            System.out.println(new String(data, 0, len));
        }
    }

    @Test
    void FileInputStreamTest2() throws IOException {
        File file = new File("c:/env/诸葛亮.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        // 核心代码
        byte[] data = new byte[2];
        while (fileInputStream.read(data) != -1) {
            System.out.println(new String(data));
        }
    }

    @Test
    void FileInputStreamTest1() throws IOException {
        File file = new File("c:/env/诸葛亮.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        // 核心代码
        int b;
        while ((b = fileInputStream.read()) != -1) {
            System.out.print((char) b);
        }
    }
}
